// Upgrade NOTE: replaced 'glstate.matrix.mvp' with 'UNITY_MATRIX_MVP'

Shader "GK/Unlit/SphereVolume" 
{
	Properties 
	{
	    _Layer01("Layer 01 (RGBA)", 2D) = "white" {}
	    _Layer02("Layer 02 (RGBA)", 2D) = "white" {}
	    _Layer03("Layer 03 (RGBA)", 2D) = "white" {}
	    _Layer04("Layer 04 (RGBA)", 2D) = "white" {}
	    _Radius("Radius", FLoat) = 1.0
	    _LayerMin("Layer min", Float) = 0.9
	    _LayerMax("Layer max", Float) = 1.0
	}
	
	SubShader 
	{	    
	    Pass 
	    {
			CGPROGRAM

			#pragma vertex vert
			#pragma fragment frag
			#include "UnityCG.cginc"
			
			uniform sampler2D _Layer01;
			uniform sampler2D _Layer02;
			uniform sampler2D _Layer03;
			uniform sampler2D _Layer04;
			
			uniform float _Radius;
			
			uniform float _LayerMin;
			uniform float _LayerMax;
			
			struct v2f 
			{
			    float4 pos : POSITION;
			    float2 uv : TEXCOORD0;
			    float2 height : TEXCOORD1;
			};
			
			float ComputeLayerTransitionPercent(float a_fCurrentDepth, int a_iLayerIndex, float a_fLayerDepth)
			{
				float fLayerBeginning;
				
				// Compute layer beginning
				fLayerBeginning = 1.0f - a_iLayerIndex * a_fLayerDepth;
				
				return (fLayerBeginning - a_fCurrentDepth)/a_fLayerDepth;
			}
			
			v2f vert (appdata_base v)
			{
			    v2f o;
			    o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
			    o.uv = TRANSFORM_UV(0);
			    
			    // Commpute the radial height
			    o.height.x = sqrt(v.vertex.x * v.vertex.x + v.vertex.y * v.vertex.y + v.vertex.z * v.vertex.z);
			    o.height.x = (clamp(o.height.x/_Radius, _LayerMin, _LayerMax) - _LayerMin)/(_LayerMax - _LayerMin);
				o.height.y = o.height.x;
				
			    return o;
			}
			
			half4 frag( v2f i ) : COLOR
			{
				float fNbTextureHeightStep = 4.0;
				float fTextureHeightStep = 1.0/fNbTextureHeightStep;
				half4 colorA = half4(0,0,0,0);
				half4 colorB = half4(0,0,0,0);
				float fLayerTransitionPercent = 0.0f;
				
				if(i.height.x >= (fTextureHeightStep * (fNbTextureHeightStep - 1)))
				{
			    	colorA = tex2D(_Layer01, i.uv);
			    	colorB = tex2D(_Layer02, i.uv);
			    	fLayerTransitionPercent = ComputeLayerTransitionPercent(i.height.x, 0, fTextureHeightStep);
			    	
			    }
			    else if(i.height.x >= (fTextureHeightStep * (fNbTextureHeightStep - 2)))
			    {
			    	colorA = tex2D(_Layer02, i.uv);
			    	colorB = tex2D(_Layer03, i.uv);
			    	fLayerTransitionPercent = ComputeLayerTransitionPercent(i.height.x, 1, fTextureHeightStep);
			    }
			    else if(i.height.x >= (fTextureHeightStep * (fNbTextureHeightStep - 3)))
			    {
			    	colorA = tex2D(_Layer03, i.uv);
			    	colorB = tex2D(_Layer04, i.uv);
			    	fLayerTransitionPercent = ComputeLayerTransitionPercent(i.height.x, 2, fTextureHeightStep);
			    }
			    else if(i.height.x >= (fTextureHeightStep * (fNbTextureHeightStep - 4)))
			    {
			    	colorA = tex2D(_Layer04, i.uv);
			    	colorB = tex2D(_Layer04, i.uv);
			    	fLayerTransitionPercent = ComputeLayerTransitionPercent(i.height.x, 3, fTextureHeightStep);
			    }	    
			    
			    return lerp(colorA, colorB, fLayerTransitionPercent);
			}
			
			ENDCG
		}

	}
}